package com.xxxx.crm.controller;

import com.xxxx.crm.base.BaseController;
import com.xxxx.crm.base.ResultInfo;
import com.xxxx.crm.exceptions.ParamsException;
import com.xxxx.crm.model.UserModel;
import com.xxxx.crm.query.UserQuery;
import com.xxxx.crm.service.UserService;
import com.xxxx.crm.utils.AssertUtil;
import com.xxxx.crm.utils.LoginUserUtil;
import com.xxxx.crm.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * @Author:王亚栋
 * @Date:2021/10/23 18:14
 * @Version:1.0
 */
@Controller
@RequestMapping("user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @ResponseBody
    public ResultInfo userLogin(String userName, String password){

        ResultInfo resultInfo = new ResultInfo();

        //调用service层的登录方法
        UserModel userModel = userService.userLogin(userName, password);

        resultInfo.setResult(userModel);

        //通过try catch捕获service层的异常

        return resultInfo;
    }

    /**
     * 修改用户密码操作
     * @param request
     * @param oldPassword
     * @param newPassword
     * @param repeatPassword
     * @return
     */
    @PostMapping("/updatePassword")
    @ResponseBody
    public ResultInfo updateUserPassword(HttpServletRequest request,
                                         String oldPassword,
                                         String newPassword,
                                         String repeatPassword){

        ResultInfo resultInfo = new ResultInfo();

        //获取session中的用户Id
        Integer userId = LoginUserUtil.releaseUserIdFromCookie(request);

        //调用service层更新密码方法
        userService.updatePassword(userId,oldPassword,newPassword,repeatPassword);

        return resultInfo;
    }

    @GetMapping("/toPasswordPage")
    public String toPasswordPage(){

        return "user/password";
    }

    /**
     * 查询所有销售人员
     * @return
     */
    @GetMapping("/queryAllSales")
    @ResponseBody
    public List<Map<String,Object>> queryAllSales(){

       return userService.queryAllSales();
    }

    /**
     * 分页多条件查询
     * @param userQuery
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public Map<String,Object> selectByParams(UserQuery userQuery){

        return userService.queryByParamsForTable(userQuery);
    }

    @GetMapping("/index")
    public String index(){

        return "user/user";
    }

    /**
     * 添加用户功能
     * @param user
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public ResultInfo addUser(User user){

        userService.addUser(user);

        return success("用户添加成功！");
    }

    @GetMapping("/toAddOrUpdateUserPage")
    public String toAddOrUpdateUserPage(Integer id,HttpServletRequest request){

        //判断id是否为空
        if (id != null){
            //通过id查询用户对象
            User user = userService.selectByPrimaryKey(id);

            request.setAttribute("userInfo",user);

        }
        return "user/add_update";
    }

    /**
     * 更新用户记录操作
     * @param user
     * @return
     */
    @PostMapping("/update")
    public ResultInfo updateUser(User user){

        userService.updateUser(user);

        return success("用户记录更新成功！");
    }

    /**
     * 用户删除记录操作
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public ResultInfo deleteUser(Integer[] ids){

        userService.deleteByIds(ids);

        return success("");
    }
}
